package 晚间作业0322;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 【中】程序设计：图书管理器，设计一个图书管理器，实现对图书进行的存储管理操作，实现功能
 * 1. 添加一本图书（书名、作者（姓名，年龄，性别）、售价）
 * 2. 删除一本图书（通过书名删除）
 * 3. 删除所有的指定作者的书（通过作者姓名删除）
 * 4. 将所有的图书按照图书售价降序排序。若售价相同，按照作者年龄升序）
 */
public class Doc14_03 {
    public static void main(String[] args) {
        BookManageSystem bookManageSystem = BookManageSystem.getInstance();
        Author zhang = new Author("zhang", 21, Author.GENDER_MALE);
        bookManageSystem.add(new Book(zhang, "book111", 52.0));
        bookManageSystem.add(new Book(zhang, "book11112", 62.0));
        bookManageSystem.add(new Book(new Author("zhao", 20, Author.GENDER_MALE), "111book", 14.0));
        bookManageSystem.add(new Book(new Author("li", 21, Author.GENDER_MALE), "222o", 12.0));
        bookManageSystem.add(new Book(new Author("lia", 20, Author.GENDER_MALE), "333o", 12.0));
        bookManageSystem.add(new Book(new Author("wang", 19, Author.GENDER_FEMALE), "333boo", 22.0));
        bookManageSystem.remove("111book");
        bookManageSystem.removeAllByAuthor(zhang.name);
        bookManageSystem.sort();
        bookManageSystem.print();
    }

    public static class BookManageSystem {
        public static BookManageSystem INSTANCE = new BookManageSystem();
        public static BookManageSystem getInstance() { return INSTANCE; }
        private List<Book> mBook;
        private BookManageSystem() { mBook = new ArrayList<>(); }

        public void add(Book book) {
            mBook.add(book);
        }

        public void remove(String bookName) {
            mBook.removeIf(book -> book.bookName.equals(bookName));
        }

        public void removeAllByAuthor(String author) {
            mBook.removeIf(book -> book.author.name.equals(author));
        }

        public void sort() {
            sort((o1, o2) -> {
                if (o1.price == o2.price) {
                    return o1.author.age - o2.author.age;
                }
                return o1.price - o2.price > 0 ? -1 : 1;
            });
        }

        public void sort(Comparator<Book> comparator) {
            mBook.sort(comparator);
        }

        public void print() {
            mBook.forEach(item -> System.out.println(item.toString()));
        }
    }

    public static class Author {
        public static final int GENDER_FEMALE = 0;
        public static final int GENDER_MALE = 1;
        private String name;
        private int age;
        private int gender;

        public Author(String name, int age, int gender) {
            this.name = name;
            this.age = age;
            this.gender = gender;
        }
    }

    public static class Book {
        private Author author;
        private String bookName;
        private double price;

        public Book(Author author, String bookName, double price) {
            this.author = author;
            this.bookName = bookName;
            this.price = price;
        }

        @Override
        public String toString() {
            return "《" + bookName + "》$" + price + ", 作者:" + author.name + ", 年龄: " + author.age;
        }
    }
}
